package my.diplom.dao.impl;

import my.diplom.dao.IResourceDAO;
import my.diplom.entity.Resource;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 14.07.2009
 * Time: 16:18:51
 * To change this template use File | Settings | File Templates.
 */
public class ResourceDAOImpl implements IResourceDAO {


    private static final Logger log = Logger.getLogger(ResourceDAOImpl.class);

    private HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    public void create(Resource resource) {
        log.debug("create resource " + resource.getHostName());
        template.save(resource);
    }

    public void update(Resource resource) {
        log.debug("update resource " + resource.getHostName());
        template.saveOrUpdate(resource);
    }

    public Resource getById(Long resourceId) {
        log.debug("get resource by id = " + resourceId);
        return (Resource) template.get(Resource.class, resourceId);
    }

     public Resource getById(Long resourceId, boolean initProjects) {
        log.debug("get resource by id = " + resourceId);
         Resource resource = (Resource) template.get(Resource.class, resourceId);
         if (initProjects){
             template.initialize(resource.getProjects());
         }
         return resource;
    }

    public List<Resource> getAll() {
        log.debug("get all resources");
        //noinspection JpaQlInspection
        return template.find("from Resource order by hostName");
    }

    public Resource getByName(String resourceName) {
        log.debug("get resource by name = " + resourceName);
        //noinspection JpaQlInspection
        return (Resource) (template.find("from Resource r where r.hostName = ?", resourceName).get(0));
    }

    public void delete(Resource resource) {
        log.debug("delete reesource");
        template.delete(resource);
    }

    @Override
    public List<Resource> getResourcesByLogin(String login) {
        return template.find("select distinct r from Resource as r join r.projects as p join p.users as u where u.login = ? and r.status.statusName = 'active'", login);

//        select DISTINCT resources.* from users
//        left join users_projects on users_projects.userId = users.userId
//        left join resources_projects on resources_projects.projectId = users_projects.projectId
//        left join resources on resources.resourceId = resources_projects.resourceId
//        where users.userId = 1

    }

    @Override
    public List<Resource> getAll(boolean initAll) {
        List<Resource> resources = template.find("from Resource order by hostName");
        if (initAll){
           for(Resource r: resources){
               template.initialize(r.getEvents());
               template.initialize(r.getProjects());
           }
        }
        return resources;
    }
}
